<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="12">
                <div class="grid-content bg-purple">
                    <el-form :model="param" :rules="rules" ref="reg" label-width="0px" class="ms-content">
                        <el-form-item prop="name">
                            <el-input v-model="param.name" placeholder="输入用户名" disabled>
                                <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="idCard">
                            <el-input v-model="param.idCard" placeholder="输入身份证号" disabled>
                                <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="card">
                            <el-input v-model="param.card" placeholder="一卡通账号" disabled>
                                <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input v-model="param.password" type="password" placeholder="输入密码">
                                <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="phone">
                            <el-input v-model="param.phone" type="text" placeholder="输入手机号">
                                <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="age">
                            <el-input v-model="param.age" placeholder="输入年龄">
                                <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="sex">
                            <el-select v-model="param.sex" placeholder="请选择学院">
                                <el-option label="男" value="男" />
                                <el-option label="女" value="女" />
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="province">
                            <el-input v-model="param.province" placeholder="输入籍贯">
                                <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="college">
                            <el-select v-model="param.college" placeholder="请选择学院">
                                <el-option v-for="item in collegeList" :key="item.id" :label="item.name" :value="item.name" />
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="nation">
                            <el-input v-model="param.nation" placeholder="输入民族">
                                <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                            </el-input>
                        </el-form-item>
                        <div class="login-btn">
                            <el-button type="primary" @click="save()">修改</el-button>
                        </div>
                    </el-form>
                </div>
            </el-col>
            <el-col :span="12">
                <div class="grid-content bg-purple"></div>
            </el-col>
        </el-row>
    </div>
</template>
<script>
export default {
    data() {
        return {
            param: {
                id: '',
                name: '',
                idCard: '',
                card: '',
                age: '',
                sex: '',
                province: '',
                college: '',
                nation: '',
                password: '',
                phone: ''
            },
            collegeList: [],
            rules: {
                name: [
                    { required: true, message: '请输入名字名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                idCard: [{ required: true, message: '请输入18位身份证号', trigger: 'blur' }],
                age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
                province: [
                    { required: true, message: '请输入籍贯', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                college: [{ required: true, message: '请选择院系', trigger: 'change' }],
                nation: [{ required: true, message: '请输入民族', trigger: 'blur' }]
            }
        };
    },
    methods: {
        // 获取所有的院系
        async allCollege() {
            const res = await this.getJSON(`/college/findall`);
            if (res.code == 200) {
                this.collegeList = res.data.records;
            } else {
                this.$message.error('院系数据加载失败');
            }
        },
        // 修改个人信息
        async save() {
            this.$refs.reg.validate(async valid => {
                if (valid) {
                    await this.putJSON(`/student/updatebyid`, this.param);
                    this.$message.success('修改成功');
                } else {
                    this.$message.error('信息输入有误');
                    return false;
                }
            });
        },
        // 获取登录的个人信息
        async stuInfo() {
            const res = await localStorage.getItem('student');
            const info = JSON.parse(res);
            this.param = info;
            console.log(this.param);
        }
    },
    created() {
        this.allCollege();
        this.stuInfo();
    }
};
</script>
<style scoped></style>
